package com.tjbaobao.videoplayol.utils;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;

import com.tjbaobao.framework.utils.ResourcesGetTools;
import com.tjbaobao.framework.utils.Tools;
import com.tjbaobao.videoplayol.message.request.UserUploadHeadRequest;
import com.tjbaobao.videoplayol.message.response.BaseResponse;

import java.io.InputStream;

/**
 * Created by TJbaobao on 2017/11/4.
 */

public class UploadHeadUtil implements ResourcesGetTools.OnResourcesGetListener{

    private ResourcesGetTools mResourcesGetTools ;
    private MyUploadAsyncTask mUploadAsyncTask ;
    private Activity activity;
    public UploadHeadUtil(Activity activity) {
        this.activity = activity;
        mResourcesGetTools = new ResourcesGetTools(activity);
        mResourcesGetTools.setOnResourcesGetListener(this);
    }

    public void util()
    {
        mResourcesGetTools.getImgByGallery();

    }

    public void onFinish(String path)
    {

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        mResourcesGetTools.onActivityResult(requestCode,resultCode,data);
    }

    @Override
    public void onSuccess(int requestCode, String path, Intent data) {
        mUploadAsyncTask = new MyUploadAsyncTask(path);
        mUploadAsyncTask.execute();
    }

    @Override
    public void onSuccess(int requestCode, Uri uri, Intent data) {

    }

    @Override
    public void onSuccess(int requestCode, InputStream inputStream, Intent data) {

    }

    @Override
    public void onFail(int requestCode, int resultCode) {

    }

    private class MyUploadAsyncTask extends UploadAsyncTask
    {

        public MyUploadAsyncTask(String path) {
            super(path);
        }

        @Override
        protected void onSuccess(String code) {
            UserUploadHeadRequest request = new UserUploadHeadRequest();
            request.setRequestCode("UserServlet");
            request.setParameter("upload_head");
            UserUploadHeadRequest.Info info = request.new Info();
            info.setCode(code);
            request.addData(info);
            new SendDataTask<UserUploadHeadRequest>(activity,request){
                @Override
                protected void onSuccess(BaseResponse response) {
                    Tools.showToast("上传成功!");
                    onFinish(path);
                }
            }.execute();
        }
        @Override
        protected void onFail() {
            Tools.showToast("上传失败!");
        }
    }

}
